using System;
using System.Collections.Generic;
using System.Text;
using XMIAddin.Xmi;
using Reflector.CodeModel;

namespace XMIAddin.ReflectorLoad
{
    public class ElementCache
    {
        private readonly List<IAssembly> _FullyLoad = new List<IAssembly>();
        private readonly UmlModel _CacheFor;
        private readonly ModelLoader _Loader;
        public readonly Dictionary<object, UmlModelElement> Elements = new Dictionary<object, UmlModelElement>();

        public ElementCache(UmlModel model, ModelLoader loader)
        {
            _CacheFor = model;
            _Loader = loader;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="identifier"></param>
        /// <param name="create"></param>
        /// <returns></returns>
        /// <remarks>
        /// <c>String</c> identifiers <c>UmlNamespace</c>.
        /// <c>ITypeDeclaration</c> identified <c>UmlClassifier</c> objects.
        /// </remarks>
        public T Locate<T>(object identifier, bool create) where T : UmlModelElement
        {
            if (Elements.ContainsKey(identifier))
            {
                UmlModelElement value = Elements[identifier];
                return value as T;
            }

            if (false == create) return null;

            ITypeDeclaration type = identifier as ITypeDeclaration;
            //if type reference try to load
            if (type != null)
            {
                return _Loader.Load(type) as T;
            }

            return null;
        }

        public void FullyLoad(IAssembly assembly)
        {
            _FullyLoad.Add(assembly);
        }

        public bool IsFullyLoad(IAssembly assembly)
        {
            return _FullyLoad.Contains(assembly);
        }
    }
}
